package command
{
   import com.greensock.TimelineMax;
   import com.greensock.core.TweenCore;
   import com.greensock.events.TweenEvent;
   
   import flash.events.Event;
   
   import jp.progression.commands.Command;
   
   /**
    * @author cjboy | cjboy1984@gmail.com
    * @date   Oct,19,2011
    * @usage
    * var cmd:TimelineMax = new TimelineMax();
    * cmd.insert(...);
    * ...
    * 
    * addCommand(
    *    new DoTimelineMax(cmd)
    * );
    */
   public class DoTimelineMax extends Command
   {
      private var cmd:TimelineMax;
      
      public function DoTimelineMax($timelineMax:TimelineMax, $delay:Number = 0, initObj:Object = null)
      {
         cmd = $timelineMax;
         cmd.stop();
         
         delay = $delay;
         
         super(myExecute, myInterrupt, initObj);
      }
      
      // --------------------- LINE ---------------------
      
      override public function executeComplete():void
      {
         super.executeComplete();
         
         if (cmd)
         {
            cmd.stop();
            cmd.kill();
            cmd = null;
         }
      }
      
      override public function clone():Command
      {
         return new DoTimelineMax(cmd, delay);
      }
      
      // ################### protected ##################
      
      // #################### private ###################
      
      private function myExecute():void
      {
         cmd.addEventListener(TweenEvent.COMPLETE, myComplete, false, int.MAX_VALUE, true);
         cmd.play();
      }
      
      private function myComplete(e:TweenEvent):void
      {
         cmd.removeEventListener(TweenEvent.COMPLETE, myComplete, false);
         // override executeComplete();
         executeComplete();
      }
      
      private function myInterrupt():void
      {
         cmd.stop();
         super.interrupt();
      }
      
      // --------------------- LINE ---------------------
      
   }
   
}